import tkinter as tk
from tkinter import scrolledtext, Button, Entry, Listbox, Message, Menu, StringVar


class TodoApp:
    def __init__(self, root):
        self.root = root
        self.root.title("待办事项")
        # 定义优先级

        # 创建菜单栏
        menubar = Menu(self.root)
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="取消", command=self.root.quit)
        menubar.add_cascade(label="菜单", menu=filemenu)
        self.root.config(menu=menubar)

        # 创建待办事项列表
        self.todo_list = Listbox(root, height=10, width=50)
        self.todo_list.grid(row=0, column=0, columnspan=2, padx=10, pady=10)

        # 添加滚动条
        scrollbar = tk.Scrollbar(root, command=self.todo_list.yview)
        scrollbar.grid(row=0, column=2, sticky='ns')
        self.todo_list.config(yscrollcommand=scrollbar.set)

        # 创建输入框和添加按钮
        self.entry = Entry(root)
        self.entry.grid(row=1, column=0, padx=10, pady=5)

        add_button = Button(root, text="添加", command=self.add_item)
        add_button.grid(row=1, column=1, padx=10, pady=5)

        # 创建删除按钮
        delete_button = Button(root, text="删除", command=self.delete_item)
        delete_button.grid(row=1, column=2, padx=10, pady=5)

        # 绑定双击事件到待办事项列表，用于删除项
        self.todo_list.bind('<<列表选择>>', self.on_select)

        # 初始化状态变量
        self.selected_index = None

        # 创建状态栏
        self.status = StringVar()
        self.status_label = Message(root, textvariable=self.status, aspect=200)
        self.status_label.grid(row=2, column=0, columnspan=3, sticky='ew')

    def add_item(self):
        item = self.entry.get()
        if item:
            self.todo_list.insert(tk.END, item)
            self.entry.delete(0, tk.END)
            self.status.set("事项添加")
        else:
            self.status.set("请添加事项")

    def delete_item(self):
        if self.selected_index is not None:
            self.todo_list.delete(self.selected_index)
            self.selected_index = None
            self.status.set("事项删除")
        else:
            self.status.set("请选择事项删除")

    def on_select(self, event):
        self.selected_index = self.todo_list.curselection()[0]


# 初始化Tkinter窗口
root = tk.Tk()
app = TodoApp(root)
root.mainloop()